#!/usr/bin/env python3  /*设定脚本文件直接能执行*/
# -*- coding:utf-8 -*-   /*设置编码*/
"""
@Author: WANDERSON M.PIMENTA(Wanderson-Magalhaes/PyOneDark_Qt_Widgets_Modern_GUI)
@Software: PyCharm 、Qt Designer and PySide6
@File: tQMainForm.py
@Time: 2024/04/03 13:32
@Function：主窗体函数
"""

from UI.cores.tFunctions import Functions
from tQQtCore import *
from UI.cores.tJsonSettings import Settings
from UI.elements.windows.tQMainWindow import *
from UI.elements.windows.tQMainWindow.tQMainWindowFunc import *

# 调整qt字体dpi为高规模和4k显示器
# os.environ["QT_FONT_DPI"] = "96"

# 如果是4k显示器启用
'os.environ["QT_SCALE_FACTOR"] = "2"'


# 主窗体
class MainWindow(QMainWindow):
    # 初始化主窗体
    def __init__(self):
        super().__init__()
        # 设置主窗口，加载自："UI\elements\tQMainWindow\tQMainWindowUI.py"
        self.ui = ctQMainWindowUI()
        # 调用布局函数
        self.ui.f_mainFormLayout(self)
        # 加载设置
        settings = Settings()
        self.settings = settings.items
        # 设置主窗口 展示/隐藏/调整大小
        self.hide_grips = True
        # 设置主窗口参数
        ctQMainWindowWidget.f_mainFormWidgetLayout(self)
        # 设置窗体大小并移动到屏幕中间位置
        self.__f_setAndMoveFormFunc(0.64, 0.68)
        # 展示主窗口
        self.show()

    # 标题栏&左侧菜单栏按键按下响应函数
    def f_titleBarAndLeftColumnBtnClicked(self):
        """
        function:  标题栏&左侧菜单栏按键按下响应函数（在tQMainWindowWidget中调用）
              in:  None
             out:  None
          return:  None
          others:  Title Bar & Left Menu Btn Clicked Response Func
        """
        # 获取被点击的按键
        btn = ctQMainWindowWidget.f_getBtnSender(self)

        # Remove Selection If Clicked By "btn_close_left_column"
        if btn.objectName() != "btn_settings":
            self.ui.leftMenuBar.f_deselectAllTab()

        # 获得标题栏的设置按键并激活
        top_settings = ctQMainWindowFunc.f_getTitleBarBtnFunc(self, "btn_top_settings")
        top_settings.f_setActiveFunc(False)

        # <editor-fold desc="加载页面">
        # 左侧菜单栏：加载主页页面
        if btn.objectName() == "btn_home":
            # 选择页面
            self.ui.leftMenuBar.f_selectOnlyOnePage(btn.objectName())
            # 加载Page_1
            ctQMainWindowFunc.f_setPageFunc(self, self.ui.mainPages.page_1)

        # 加载控件展示页面
        if btn.objectName() == "btn_widgets":
            # 选择页面
            self.ui.leftMenuBar.f_selectOnlyOnePage(btn.objectName())
            # 加载Page_2
            ctQMainWindowFunc.f_setPageFunc(self, self.ui.mainPages.page_2)

        # 加载用户选择页面
        if btn.objectName() == "btn_add_user":
            # 选择页面
            self.ui.leftMenuBar.f_selectOnlyOnePage(btn.objectName())
            # 加载Page_3
            ctQMainWindowFunc.f_setPageFunc(self, self.ui.mainPages.page_3)
        # </editor-fold>

        # <editor-fold desc="信息按键">
        if btn.objectName() == "btn_info":
            # 检查左列是否可见
            if not ctQMainWindowFunc.f_leftColumnIsVisibleFunc(self):
                self.ui.leftMenuBar.f_selectOnlyOneTab(btn.objectName())
                # 显示/隐藏
                ctQMainWindowFunc.f_toggleLeftColumnFunc(self)
                self.ui.leftMenuBar.f_selectOnlyOneTab(btn.objectName())
            else:
                if btn.objectName() == "btn_close_left_column":
                    self.ui.leftMenuBar.f_deselectAllTab()
                    # 显示/隐藏
                    ctQMainWindowFunc.f_toggleLeftColumnFunc(self)

                self.ui.leftMenuBar.f_selectOnlyOneTab(btn.objectName())

            # Change Left Column Menu
            if btn.objectName() != "btn_close_left_column":
                ctQMainWindowFunc.f_setLeftColumnMenu(self,
                                                      menu=self.ui.leftColumn.menus.menu_2,
                                                      title="Info tab",
                                                      icon_path=Functions.f_getSvgIconPath("icon_info.svg")
                                                      )
        # </editor-fold>

        # <editor-fold desc="设置按键">
        if btn.objectName() == "btn_settings" or btn.objectName() == "btn_close_left_column":
            # CHECK IF LEFT COLUMN IS VISIBLE
            if not ctQMainWindowFunc.f_leftColumnIsVisibleFunc(self):
                # Show / Hide
                ctQMainWindowFunc.f_toggleLeftColumnFunc(self)
                self.ui.leftMenuBar.f_selectOnlyOneTab(btn.objectName())
            else:
                if btn.objectName() == "btn_close_left_column":
                    self.ui.leftMenuBar.f_deselectAllTab()
                    # Show / Hide
                    ctQMainWindowFunc.f_toggleLeftColumnFunc(self)
                self.ui.leftMenuBar.f_selectOnlyOneTab(btn.objectName())

            # Change Left Column Menu
            if btn.objectName() != "btn_close_left_column":
                ctQMainWindowFunc.f_setLeftColumnMenu(self,
                                                      menu=self.ui.leftColumn.menus.menu_1,
                                                      title="Settings Left Column",
                                                      icon_path=Functions.f_getSvgIconPath("icon_settings.svg")
                                                      )
        # </editor-fold>

        # <editor-fold desc="标题栏设置按键">
        # 如果选择的按键为标题栏的设置按键
        if btn.objectName() == "btn_top_settings":
            #  获取右列是否可见
            if not ctQMainWindowFunc.f_rightColumnIsVisibleFunc(self):
                # 按键设置为激活
                btn.f_setActiveFunc(True)
                # 显示右列
                ctQMainWindowFunc.f_toggleRightColumnFunc(self)
            else:
                # 按键设置为休眠
                btn.f_setActiveFunc(False)
                # 隐藏右列
                ctQMainWindowFunc.f_toggleRightColumnFunc(self)
            # 获取左侧菜单栏的设置按键
            top_settings = ctQMainWindowFunc.f_getLeftMenuBarBtnFunc(self, "btn_settings")
            # 将左侧菜单栏的设置按键设置为休眠
            top_settings.f_setTabActive(False)
        # </editor-fold>

        # print(f"Button {btn.objectName()}, clicked!")

    # 标题栏&左侧菜单栏按键释放响应函数
    def f_titleBarAndLeftColumnBtnReleased(self):
        """
        function:  标题栏&左侧菜单栏按键按下响应函数
              in:  None
             out:  None
          return:  None
          others:  Title Bar & Left Menu Btn Released Response Func
        """
        # 获取被点击的按键
        btn = ctQMainWindowWidget.f_getBtnSender(self)
        # print(f"Button {btn.objectName()}, released!")

    # <editor-fold desc="重写事件">
    # 重写调整大小事件
    def resizeEvent(self, event):
        ctQMainWindowWidget.f_resizeGrips(self)

    # 重写鼠标点击事件
    def mousePressEvent(self, event):
        # 设置拖拽窗口
        self.dragPos = event.globalPosition().toPoint()

    # </editor-fold>

    # <editor-fold desc="用到的私有方法">
    # 设置窗体大小并移动到屏幕中间位置
    def __f_setAndMoveFormFunc(self, widthRatio, heightRatio):
        """
        function:  移动窗体到屏幕中间位置
              in:  widthRatio：宽度比例
                   heightRatio：高度比例
             out:  None
          return:  None
          others:  Set Form Size And Move To The Middle Of The Screen
        """
        # 获取缩放比率
        self.__m_ratio = QGuiApplication.primaryScreen().devicePixelRatio()
        # 获取屏幕的宽
        self.__m_width = QGuiApplication.primaryScreen().availableGeometry().width()
        # 获取屏幕的高
        self.__m_height = QGuiApplication.primaryScreen().availableGeometry().height()
        self.__m_widthRatio = widthRatio  # 0.645
        self.__m_heightRatio = heightRatio  # 0.72
        # 设置窗体大小
        self.setGeometry(0, 0, self.__m_widthRatio * self.__m_width, self.__m_heightRatio * self.__m_height)
        # 获得窗口的数据
        q_rect = self.frameGeometry()
        # 获得可视窗口的中心点坐标
        center_point = QGuiApplication.primaryScreen().geometry().center()
        # 将QRect移动到中心点
        q_rect.moveCenter(center_point)
        # 将窗口本身移动到中心点
        self.move(q_rect.topLeft())

    # </editor-fold>


# 设置初始类和QApplication类的其他参数s
if __name__ == "__main__":
    app = QApplication(sys.argv)
    app.setWindowIcon(QIcon("icon.ico"))
    window = MainWindow()
    sys.exit(app.exec())
